{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github"
      },
      "source": [
        "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/camenduru/text-to-video-synthesis-colab/blob/main/text_to_video_synthesis_diffusers.ipynb)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "#https://github.com/huggingface/diffusers/pull/2738 modified\n",
        "!pip install git+https://github.com/huggingface/diffusers transformers accelerate imageio[ffmpeg] -U\n",
        "!pip install -q torch==1.13.1+cu116 torchvision==0.14.1+cu116 torchaudio==0.13.1 torchtext==0.14.1 torchdata==0.5.1 --extra-index-url https://download.pytorch.org/whl/cu116 -U"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "n2ibl97e2yM4"
      },
      "outputs": [],
      "source": [
        "import torch\n",
        "from diffusers import DiffusionPipeline, DPMSolverMultistepScheduler\n",
        "from diffusers.utils import export_to_video\n",
        "from IPython.display import HTML\n",
        "from base64 import b64encode\n",
        "import datetime\n",
        "\n",
        "pipe = DiffusionPipeline.from_pretrained(\"damo-vilab/text-to-video-ms-1.7b\", torch_dtype=torch.float16, variant=\"fp16\")\n",
        "pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)\n",
        "pipe.enable_model_cpu_offload()\n",
        "pipe.enable_vae_slicing()\n",
        "\n",
        "!mkdir /content/videos"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "prompt = 'Darth Vader surfing a wave' #@param {type:\"string\"}\n",
        "negative_prompt = \"low quality\" #@param {type:\"string\"}\n",
        "num_frames = 30 #@param {type:\"raw\"}\n",
        "video_frames = pipe(prompt, negative_prompt=negative_prompt, num_inference_steps=25, num_frames=num_frames).frames\n",
        "output_video_path = export_to_video(video_frames)\n",
        "\n",
        "new_video_path = f'/content/videos/{datetime.datetime.now().strftime(\"%Y-%m-%d_%H:%M:%S\")}.mp4'\n",
        "!ffmpeg -y -i {output_video_path} -c:v libx264 -c:a aac -strict -2 {new_video_path} >/dev/null 2>&1\n",
        "\n",
        "print(output_video_path, '->', new_video_path)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "!cp {new_video_path} /content/videos/tmp.mp4\n",
        "mp4 = open('/content/videos/tmp.mp4','rb').read()\n",
        "\n",
        "decoded_vid = \"data:video/mp4;base64,\" + b64encode(mp4).decode()\n",
        "HTML(f'<video width=400 controls><source src=\"{decoded_vid}\" type=\"video/mp4\"></video>')"
      ]
    }
  ],
  "metadata": {
    "accelerator": "GPU",
    "colab": {
      "provenance": []
    },
    "gpuClass": "standard",
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
